<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- <script type="text/javascript">
        var num = Number('123');
        var num1 = Number(''); // 0
        var num2 = Number(true);

        var num3 = Number(null);
        var num4 = Number(false);
        var num5 = Number(undefined); // 转换成NaN，不管可不可以转换成数字，但最后都是数字类型
        var num6 = Number('1a');
        console.log(typeof(num) + ':', num);
        console.log(typeof(num2) + ':', num2);
        console.log(typeof(num3) + ':', num3);
        console.log(typeof(num4) + ':', num4);
        console.log(typeof(num5) + ':', num5);
        console.log(typeof(num6) + ':', num6);
    </script> -->

    <script>
        // parseInt是转换成10进制
        var demo = "123.9px"; //转换成123，直接非数字位截断
        // var demo = true;   //转换成NaN
        // var demo = null;   // 转换成NaN
        var res = parseInt(demo);  //转换成整数，直接取整，不四舍五入
        console.log(typeof(res) + ':', res);

        var res1 = parseInt(demo, 16);//以16进制为基底，转换成10进制
        console.log(typeof(res1) + ':', res1);

        var res2 = parseInt("1010101", 2);
        console.log(typeof(res2) + ':', res2);

        var res3 = parseInt(3, 2);
        console.log(typeof(res3) + ':', res3); //NaN

        var res4 = parseInt(3, 0);  // 0进制没有意义，结果或者报错，或者相当于没写，或者NaN
        console.log(typeof(res4) + ':', res4); //3
    </script>

    <script>
        var demo = "100.2abcd";// 到除了第一个点的非数字位截止
        var num = parseFloat(demo);
        console.log(typeof (num) + " : ", num);

    </script>


    <script>
        var demo = 1234.5;
       // var demo = undefined; //转换成字符串
        var num = String(demo);
        console.log(typeof (num) + " : ", num);


        var demo = 1234.5;
        var demo = undefined;
        var num = Boolean(demo);
        console.log(typeof (num) + " : ", num);

    </script>

    <!-- toString() -->
    <script>
        var demo = 123;
        // undefined 和 null 没有这个方法
        var num = demo.toString();
        var num1 = demo.toString(16); //以10进制为基底，转换成目标进制
        console.log(typeof (num) + " : ", num);
        console.log(typeof (num1) + " : ", num1);
    </script>

    <!-- 把一个二进制的数转换成16进制 -->
    <script>
        var num = 1010;
        var test = parseInt(num, 2);//转换成10进制
        console.log(test.toString(16));
    </script>
</body>

</html>